package controller;

import java.io.IOException;
import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import model.dao.SprintDao;
import model.dao.TaskDao;
import model.domain.Project;
import model.domain.Sprint;
import model.domain.Status;
import model.domain.Task;
import org.primefaces.event.DragDropEvent;

public class taskboardBean implements Serializable {


    public taskboardBean() {
        this.selectedtask = new Task();
    }
    
    private Project project;
    private Task selectedtask;

    public void setProject(Project object){
        this.project = object;
    }

    public Project getProject(){
	return this.project;
    }

    public Task getSelectedtask(){
        return this.selectedtask;
    }

    public void setSelectedtask(Task object){
        this.selectedtask.setTaskID(object.getTaskID());
        this.selectedtask.setStatus(object.getStatus());
        this.selectedtask.setDescription(object.getDescription());
        this.selectedtask.setUser(object.getUser());
        this.selectedtask.setStart(object.getStart());
        this.selectedtask.setEnd(object.getEnd());
        this.selectedtask.setEstimatedEffort(object.getEstimatedEffort());
        this.selectedtask.setComments(object.getComments());
        this.selectedtask.setProductbacklog(object.getProductbacklog());
    }


    public List<Task> getToDoTasks() throws ParseException{
        Sprint currentSprint = this.getCurrentSprint();
        TaskDao home = new TaskDao();
        Status status = new Status();
        status.setStatusID(1);
        return home.findBySprintAndStatus(currentSprint,status);
    }

    public List<Task> getDoingTasks() throws ParseException{
        Sprint currentSprint = this.getCurrentSprint();
        TaskDao home = new TaskDao();
        Status status = new Status();
        status.setStatusID(2);
        return home.findBySprintAndStatus(currentSprint,status);
    }

    public List<Task> getDoneTasks() throws ParseException{
        Sprint currentSprint = this.getCurrentSprint();
        TaskDao home = new TaskDao();
        Status status = new Status();
        status.setStatusID(3);
        return home.findBySprintAndStatus(currentSprint,status);
    }


    public void onDrop(DragDropEvent event) throws IOException {

            Task task = (Task) event.getData();

            Status status = new Status();

            if(event.getDropId().contains("todoPanel")){
                task.setEnd(null);
                status.setStatusID(1);
                status.setDescription("pending");
            }
            else if(event.getDropId().contains("doingPanel")){
                task.setEnd(null);
                status.setStatusID(2);
                status.setDescription("in_progress");
            }
            else if(event.getDropId().contains("donePanel")){
                Calendar cal = Calendar.getInstance();
                cal.set(Calendar.HOUR, 0);
                cal.set(Calendar.MINUTE, 0);
                cal.set(Calendar.SECOND, 0);

                task.setEnd(cal.getTime());
                status.setStatusID(3);
                status.setDescription("completed");
            }

            task.setStatus(status);

            TaskDao home = new TaskDao();
            home.insertOrUpdate(task);

            FacesContext.getCurrentInstance().getExternalContext().redirect("Taskboard.xhtml");
    }

    public Sprint getCurrentSprint() throws ParseException{
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

        Calendar cal = Calendar.getInstance();
        Date now = dateFormat.parse(dateFormat.format(cal.getTime()));
        SprintDao home = new SprintDao();
        Sprint currentSprint = home.findByDate(now, this.project);
        return currentSprint;
    }


}  